package com.graduation.check.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Files;
import java.security.MessageDigest;

@Component
public class FileUtil {

    /**
     * 从文件中提取文本内容
     * @param filePath 文件路径
     * @return 提取的文本内容
     */
    public String extractText(String filePath) {
        try {
            if (filePath.toLowerCase().endsWith(".pdf")) {
                return extractTextFromPdf(filePath);
            } else if (filePath.toLowerCase().endsWith(".docx")) {
                return extractTextFromDocx(filePath);
            } else if (filePath.toLowerCase().endsWith(".txt")) {
                return new String(Files.readAllBytes(new File(filePath).toPath()));
            } else {
                throw new UnsupportedOperationException("不支持的文件格式");
            }
        } catch (Exception e) {
            throw new RuntimeException("文件内容提取失败: " + e.getMessage(), e);
        }
    }

    /**
     * 从PDF文件中提取文本
     * @param filePath PDF文件路径
     * @return 提取的文本内容
     */
    private String extractTextFromPdf(String filePath) throws Exception {
        // PDFBox 3.x 版本的加载方式
        try (PDDocument document = PDDocument.load(new File(filePath))) {
            PDFTextStripper stripper = new PDFTextStripper();
            return stripper.getText(document);
        }
    }

    /**
     * 从Word文档中提取文本
     * @param filePath Word文档路径
     * @return 提取的文本内容
     */
    private String extractTextFromDocx(String filePath) throws Exception {
        try (FileInputStream fis = new FileInputStream(filePath);
             XWPFDocument document = new XWPFDocument(fis);
             XWPFWordExtractor extractor = new XWPFWordExtractor(document)) {
            return extractor.getText();
        }
    }

    /**
     * 计算文件的MD5哈希值
     * @param bytes 文件字节数组
     * @return MD5哈希字符串
     */
    public String calculateMD5(byte[] bytes) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(bytes);
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (Exception e) {
            throw new RuntimeException("MD5计算失败", e);
        }
    }

    /**
     * 获取文件扩展名
     * @param filename 文件名
     * @return 文件扩展名
     */
    public String getFileExtension(String filename) {
        return StringUtils.substringAfterLast(filename, ".");
    }

    private final ObjectMapper objectMapper = new ObjectMapper();

    public ObjectMapper getObjectMapper() {
        return objectMapper;
    }
}